package com.cheetah.start.system.common.controller;

import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.config.property.UpLoadProprety;
import com.cheetah.start.common.constant.CommonConst;
import com.cheetah.start.common.enums.FileStatusEnum;
import com.cheetah.start.common.exception.MyFileException;
import com.cheetah.start.common.utils.MyFileUtil;
import com.cheetah.start.system.common.async.FileAsync;
import com.cheetah.start.system.file.domain.SysFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

/**
 * 通用
 *
 * @author xjhmwj
 * @date 2021/6/11 17:24
 */
@Slf4j
@RestController
@RequestMapping("/common")
public class CommonController extends ResultController {


    @Autowired
    private UpLoadProprety upLoadProprety;
    @Resource
    private FileAsync sysFileAsync;
    @Value("${server.servlet.context-path}")
    private String suffix;

    /**
     * 通用上传
     *
     * @param file 文件
     * @return {@link Result}
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws MyFileException {
        MyFileUtil.checkCommonSuffix(file);
        String SUFFIX = suffix;
        String dateFileName = MyFileUtil.getDateFileName(file);
        String fullPath = upLoadProprety.getPath() + dateFileName;
        File fileHave = new File(fullPath);
        if (!fileHave.getParentFile().exists()) {
            boolean mkdirs = fileHave.getParentFile().mkdirs();
            if (!mkdirs) {
                log.error("创建文件夹失败:[{}]", fileHave.getParent());
            }
        }
        try {
            file.transferTo(fileHave);
        } catch (IOException e) {
            e.printStackTrace();
            throw new MyFileException("文件转换存储失败");
        }
        String originalFilename = file.getOriginalFilename();
        long size = file.getSize();
        String hostFullPath = upLoadProprety.getProfileHost() + dateFileName;
        if ("/".equals(suffix)) {
            SUFFIX = "";
        }
        String hostHalfPath = SUFFIX + CommonConst.RESOURCE_PREFIX_INCLUDE + dateFileName;
        hostFullPath = hostFullPath.replace(CommonConst.RESOURCE_PREFIX, SUFFIX + CommonConst.RESOURCE_PREFIX);
        HashMap<String, Object> res = new HashMap<>();
        res.put("full", hostFullPath);
        res.put("half", hostHalfPath);
        // 文件大小 KB
        res.put("size", size / 1000);
        // 原文件名
        res.put("name", originalFilename);
        SysFile sysFile = new SysFile();
        sysFile.setFileSize((int) (size / 1000));
        sysFile.setFileName(file.getOriginalFilename());
        sysFile.setFilePath(hostFullPath);
        sysFile.setFileNowName(fileHave.getName());
        sysFile.setFileUrl(hostHalfPath);
        sysFile.setFileRealPath(fullPath);
        sysFile.setCreateTime(new Date());
        // 默认使用中
        sysFile.setFileStatus(FileStatusEnum.USE.getCode());
        sysFileAsync.record(sysFile);
        return success(res);
    }

}
